<template>
  <div class="login-container">
    <van-nav-bar title="登录" left-arrow @click-left="$router.back()" />
    
    <div class="login-form">
      <div class="logo">
        <h2>创享云</h2>
        <p>灵活用工平台</p>
      </div>
      
      <van-form @submit="onSubmit">
        <van-field
          v-model="loginForm.phone"
          name="phone"
          label="手机号"
          placeholder="请输入手机号"
          :rules="[{ required: true, message: '请输入手机号' }]"
        />
        
        <van-field
          v-model="loginForm.password"
          type="password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请输入密码' }]"
        />
        
        <div style="margin: 16px;">
          <van-button round block type="primary" native-type="submit" :loading="loading">
            登录
          </van-button>
          
          <div class="register-link">
            <span @click="$router.push('/register')">没有账号？立即注册</span>
          </div>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const loginForm = reactive({
  phone: '',
  password: ''
})

const onSubmit = async (values) => {
  loading.value = true
  try {
    await userStore.loginAction(values)
    router.replace('/')
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: #f5f5f5;
}

.login-form {
  padding: 40px 20px;
}

.logo {
  text-align: center;
  margin-bottom: 40px;
}

.logo h2 {
  font-size: 32px;
  color: #1989fa;
  margin-bottom: 8px;
}

.logo p {
  font-size: 16px;
  color: #666;
}

.register-link {
  margin-top: 20px;
  text-align: center;
  color: #1989fa;
  font-size: 14px;
}
</style>